﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace wms_wpf.Extend
{
    public static class BarTenderPrint
    {


        public static BarTender.Application btApp = new BarTender.Application();
        /// <summary>
        /// BarTender运行软件
        /// </summary>
        public static BarTender.Format btFormat = new BarTender.Format();
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="printerName">打印机名称</param>
        /// <param name="btFileName">打印文件</param>
        /// <param name="dict">内容字典</param>
        /// <param name="CopiesOfLabel">打印数量</param>
        public static async Task<bool>  Print(string printerName, string btFileName, Dictionary<string, string> dict, int CopiesOfLabel)
        {
            try
            {
                var btApp = new BarTender.Application();
                btFormat = btApp.Formats.Open(btFileName);
                btFormat.PrintSetup.Printer = printerName;
                foreach (var item in dict)
                {
                    btFormat.SetNamedSubStringValue(item.Key, item.Value);
                }//加了内容字典就打不出来了
                btFormat.PrintSetup.IdenticalCopiesOfLabel = CopiesOfLabel;
                btFormat.PrintOut(false, false);
                btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
                Marshal.ReleaseComObject(btFormat);
                btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);
                Marshal.ReleaseComObject(btApp);
                GC.Collect(); // 强制垃圾回收
                GC.WaitForPendingFinalizers(); // 等待终结器完成
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("【打印机" + printerName + "】", ex.Message);
                return false;
            }

        }

        static string printer = ConfigurationManager.AppSettings["printer"];
        public static async void Label(string MaterialCode, string MaterialName,  string batch, string quantity, string Unit, string order, string Consumer_MaterialId,string OrderBatch)
        {
            try
            {
                bool Falg = false;
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "标签.btw";
                if (string.IsNullOrEmpty(Consumer_MaterialId))
                {
                    return;
                }

                string 物料编码 = MaterialCode;
                string 物料描述 = MaterialName;
                string 物料批次 = batch;
                string 拣配数量 = quantity;
                string 物料单位 = Unit;
                string 工单号 = order;
                string 物料标签ID = Consumer_MaterialId;
                string 工单批次 = OrderBatch;
                //传递打印内容，Key-Vaule
                Dictionary<string, string> dic = new Dictionary<string, string>()
                     {

                            {"物料编码",物料编码},
                            {"物料描述",物料描述},
                            {"物料批次",物料批次},
                            {"拣配数量",拣配数量},
                            {"物料单位",物料单位},
                            {"工单号",工单号 },
                            {"物料标签ID",物料标签ID},
                            {"工单批次",工单批次 },
                     };
                Falg = await BarTenderPrint.Print(printer, filePath, dic, 1);//表示有一个名称为SN的打印机，然后打印模板的路径，打印内容，打印数量
            }
            catch (Exception ex)
            {
                MessageBox.Show( ex.Message+"打印机"+ printer);
           
            }

        }

    }
}
